package classes.view.component
{
	import classes.controller.DAOSendFlash;
	import classes.controller.DAOSendJS;
	import classes.controller.EClick;
	import classes.controller.ESendJS;
	import classes.controller.VONullObject;
	import classes.model.VOIsLocal;
	import classes.model.VOUserInfo;
	
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Quart;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.filters.ColorMatrixFilter;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class ListObject extends MovieClip
	{
		public var _area:Sprite;
		
		private var _no:TextField;
		private var _type:TextField;
		private var _tintClip:MovieClip;
		private var _security:MovieClip;
		private var _DBIcon:MovieClip;
		private var _title:TextField;
		private var _roomName:TextField;
		private var _inPeople:TextField;
		private var _totalPeople:TextField;
		private var _roomState:MovieClip;
		private var _grey_tint:MovieClip;
		private var _bg:Sprite;
		private var _id:int;
		private var _gameIng:Sprite;
		private var _arrow:Sprite;
		private var _passInput:ListObject_password;
		private var _deem:Sprite;
		private var _pw:String;
		private var _white:Sprite;
		private var _roomFull:Sprite;
		private var _gameIngClip:Sprite;
		private var _noRoom:Sprite;
		
		public function ListObject()
		{
			super();
			
			setLayOut();
			defaultSetting();
			initListener();
		}
		public function setListObj($id:int, $type:String, $db:String, $title:String, $subtitle:String, $peoplenow:String, $peoplemax:String, $roomState:String, $secret:int):void
		{
			
			this._id 					= $id;
			this._no.text 				= VONullObject.getLoadInstance().nullText(String($id));
			this._type.text 			= VONullObject.getLoadInstance().nullText($type);
			this._title.text 			= VONullObject.getLoadInstance().nullText($title);
			this._roomName.text 		= VONullObject.getLoadInstance().nullText($subtitle);
			this._inPeople.text 		= VONullObject.getLoadInstance().nullText($peoplenow);
			this._totalPeople.text 		= VONullObject.getLoadInstance().nullText($peoplemax);
			
			if($roomState =="ing"){
				this._roomState.room_txt.text 	= '게임중';
				removeListObj();
				ingMode();
			}else if($roomState =="wait"){
				this._roomState.room_txt.text 	= '대기중';
				init();
			}else{
				this._roomState.room_txt.text 	= '대기중';
				init();
			}
				
			_DBIcon.db_txt.db_text.text 		= VONullObject.getLoadInstance().nullText($db); 
			
			this._security.gotoAndStop($secret+1);
		}
		
		public function get roomID():int
		{
			return this._id;
		}
		public function get password():String
		{
			return this._pw;
		}
		
		public function removeListObj():void
		{
			removeInit();
		}

		private function initListener():void
		{
			//DAOSendJS.getLoadInstance().addEventListener(ESendJS.EVENT_JOINROOMSTATE,	onJoinRoomState);
		}

		private function setLayOut():void
		{
			this._grey_tint		= this.getChildByName('grey_mc')					as MovieClip;
			this._tintClip		= this.getChildByName('tint_mc')					as MovieClip;
			this._no 			= this._grey_tint.getChildByName('no_txt')			as TextField;
			this._type			= this._grey_tint.getChildByName('type_txt')		as TextField;
			this._security		= this._tintClip.getChildByName('security_mc')		as MovieClip;
			this._DBIcon		= this.getChildByName('db_mc')						as MovieClip;
			this._title			= this._tintClip.getChildByName('title_txt')		as TextField;
			this._roomName		= this._tintClip.getChildByName('roomname_txt')		as TextField;
			this._inPeople		= this._tintClip.getChildByName('inpeople_txt')		as TextField;
			this._totalPeople	= this._tintClip.getChildByName('totalPeople_txt')	as TextField;
			this._roomState		= this.getChildByName('room_mc')					as MovieClip;
			this._area			= this.getChildByName('area_mc')					as Sprite;
			this._bg			= this.getChildByName('bg_mc')						as Sprite;
			this._gameIng		= this.getChildByName('gameing_mc')					as Sprite;
			this._arrow			= this.getChildByName('arrow_mc')					as Sprite;
			this._deem			= this.getChildByName('deem_mc')					as Sprite;
			this._passInput		= this.getChildByName('pass_input')					as ListObject_password;
			this._white			= this.getChildByName('white_mc')					as Sprite;
			this._roomFull		= this.getChildByName('room_full_mc')				as Sprite;
			this._gameIngClip	= this.getChildByName('game_ing_mc')				as Sprite;
			this._noRoom		= this.getChildByName('no_room_mc')					as Sprite;
		}
		
		private function init():void
		{
			this._area.buttonMode 					= true;
			this._area.addEventListener(MouseEvent.MOUSE_OVER,	onMouseOverHandler);
		}
		private function removeInit():void
		{
			this._area.buttonMode 					= false;
			this._area.removeEventListener(MouseEvent.MOUSE_OVER,	 onMouseOverHandler);
			this._area.removeEventListener(MouseEvent.MOUSE_OUT,	 onMouseOutHandler);
			this._area.removeEventListener(MouseEvent.CLICK,		 onClickHandler);
		}
		
		private function onMouseOverHandler(e:MouseEvent):void
		{
			TweenMax.killTweensOf(_roomState, false);
			this._roomState.room_txt.text 			= '입장하기';
			this._roomState.x  	= 180;
			this._roomState.alpha  = 0;
			_arrow.x  = 230 ;
			_arrow.alpha = 0;
			
			this._area.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
			TweenMax.to(_grey_tint, 0.45, {tint:0xe5e5e5, ease:Quart.easeInOut});
			TweenMax.to(_tintClip, 0.45, {tint:0xffffff, ease:Quart.easeInOut});
			TweenMax.to(_arrow, 0.45, {alpha:1, x:169, tint:0xffffff, ease:Quart.easeOut});
			TweenMax.to(_roomState, 0.3, {delay:0.2, alpha:1, x:183, tint:0xef443d, ease:Quart.easeIn});
			TweenMax.to(_bg, 0.3, {alpha:1, ease:Quart.easeInOut});
			
			this._area.addEventListener(MouseEvent.CLICK,	onClickHandler);
		}
		
		private function onMouseOutHandler(e:MouseEvent):void
		{
			TweenMax.killTweensOf(_roomState, false);
			this._roomState.room_txt.text 	= '대기중';
			this._roomState.alpha  = 0;
			this._roomState.x  	= 180;
			
			if(_roomFull.visible != false) TweenMax.to(_roomFull, 0.4, {autoAlpha:0, ease:Quart.easeInOut});
			if(_gameIngClip.visible != false) TweenMax.to(_gameIngClip, 0.4, {autoAlpha:0, ease:Quart.easeInOut});
			
			TweenMax.to(_grey_tint, 0.45, {removeTint:true, ease:Quart.easeInOut});
			TweenMax.to(_arrow, 0.45, {removeTint:true, ease:Quart.easeInOut});
			TweenMax.to(_tintClip, 0.45, {removeTint:true, ease:Quart.easeInOut});
			TweenMax.to(_roomState, 0.45, {alpha:1, x:183, removeTint:true, ease:Quart.easeInOut});
			TweenMax.to(_bg, 0.3, {alpha:0, ease:Quart.easeInOut});
			this._area.removeEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
			this._area.removeEventListener(MouseEvent.CLICK,	onClickHandler);
		}
		
		private function ingMode():void
		{
			this._gameIng.visible					= true;
			this.mouseChildren 						= false;
			this.mouseEnabled 						= false;
			TweenMax.to(_grey_tint, 0, {tint:0xb6b4b4});
			TweenMax.to(_tintClip, 0, {tint:0xb6b4b4});
			TweenMax.to(_arrow, 0, {tint:0xb6b4b4});
			TweenMax.to(_roomState, 0, {tint:0xf7f7f7});
			
			var b:Number = 1 / 3; var c:Number = 1 - (b * 2); 
			var matrix:Array = [c, b, b, 0, 0, b, c, b, 0, 0, b, b, c, 0, 0, 0, 0, 0, 1, 0]; 
			var matrixFilter:ColorMatrixFilter = new ColorMatrixFilter(matrix); 
			_DBIcon.filters = [matrixFilter];
			_DBIcon.alpha = 0.3;
		}
		
		private function onClickHandler(e:MouseEvent):void
		{
			removeInit();
			DAOSendJS.getLoadInstance().addEventListener(ESendJS.EVENT_JOINROOMSTATE,	onJoinRoomState);			
			var secret:int = _security.currentFrame-1;
			if(secret==1){
				
				if(VOIsLocal.getLocalInstance().isLocal == "FILE") joinRoomState = 1;
				//dispatchEvent(new EClick('', EClick.EVENT_CLICK));
				
				var obj:Object;
				obj = {
					"data":[
						{
							"id":VOUserInfo.getDataInstance().data.id
							,"roomID":roomID
							,"password":""
						}
					]
				}
					
				DAOSendFlash.getLoadInstance().setData("joinRoom", obj);
				
				//ExternalInterface.call('alert', '1');
				
			}else{
				
				this._passInput.switch_cont = true;
				TweenMax.to(_deem, 0.5, {autoAlpha:0.8});
				
				_arrow.x  = 245 ;
				TweenMax.to(_arrow, 0.45, {x:169, ease:Quart.easeOut});
				
				
				this._roomState.room_txt.text 	= '입력하기';
				this._roomState.alpha  = 0;
				this._roomState.x  	= 180;
				
				//TweenMax.killTweensOf(_roomState, false);
				TweenMax.to(_roomState, 0.45, {delay:0.2, alpha:1, x:183, ease:Quart.easeIn});
				
				this._area.visible      = false;
				
				this._arrow.buttonMode = true;
				this._arrow.addEventListener(MouseEvent.CLICK,		onPassDispatch);
				this._passInput.addEventListener("enterKey_in",		onPassDispatch);
				function onPassDispatch(e:*):void
				{
					var obj:Object;
					obj = {
						"data":[
							{
								"id":VOUserInfo.getDataInstance().data.id
								,"roomID":roomID
								,"password":_passInput.pass
							}
						]
					}
					DAOSendFlash.getLoadInstance().setData("joinRoom", obj);	
				}
				
				this.addEventListener(MouseEvent.ROLL_OUT,			passOutHandler);
				function passOutHandler(e:MouseEvent):void
				{
					DAOSendJS.getLoadInstance().removeEventListener(ESendJS.EVENT_JOINROOMSTATE,	onJoinRoomState);
					
					_area.visible      = true;
					_arrow.buttonMode = false;
					_area.addEventListener(MouseEvent.MOUSE_OVER,	onMouseOverHandler);
					
					_arrow.removeEventListener(MouseEvent.CLICK,		onPassDispatch);
					_passInput.removeEventListener("enterKey_in",		onPassDispatch);
					
					_roomState.room_txt.text 	= '대기중';
					_roomState.alpha  = 0;
					_roomState.x  	= 186;
					
					_passInput.switch_cont = false;
					
					TweenMax.killTweensOf(_roomState, false);
					
					TweenMax.to(_grey_tint, 0.45, {removeTint:true, ease:Quart.easeInOut});
					TweenMax.to(_arrow, 0.45, {removeTint:true, ease:Quart.easeInOut});
					TweenMax.to(_tintClip, 0.45, {removeTint:true, ease:Quart.easeInOut});
					TweenMax.to(_roomState, 0.45, {alpha:1, x:183, removeTint:true, ease:Quart.easeInOut});
					TweenMax.to(_bg, 0.3, {alpha:0, ease:Quart.easeInOut});
					TweenMax.to(_deem, 0.3, {autoAlpha:0});
				}
			}
		}
		private function onJoinRoomState(e:ESendJS):void
		{
			joinRoomState = e._obj.roomState
		}
		
		
		private var _sortNum:int;
		public function set sortNum($sortNum:int):void
		{
			_sortNum = $sortNum;
		}
		private var _page:int=1;
		public function set page($page:int):void
		{
			_page = $page;
		}
		
		

		
		
		
		private function set joinRoomState($state:int):void
		{
			
			trace(_sortNum +">>>>"+_page);
			
			DAOSendJS.getLoadInstance().removeEventListener(ESendJS.EVENT_JOINROOMSTATE,	onJoinRoomState);
			
			switch($state){
				case 1:
					//입장.
					dispatchEvent(new EClick('', EClick.EVENT_CLICK));
					break;
				case 2:
					//비번틀림.
					_passInput.collect = false;
					break;
				case 3:
					//인원수 초과.
					TweenMax.to(_roomFull, 0.45, {autoAlpha:1, ease:Quart.easeInOut});
					this._area.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
					reroomList();
					init();
					
					break;
				case 4:
					//게임진행.
					TweenMax.to(_gameIngClip, 0.45, {autoAlpha:1, ease:Quart.easeInOut});
					this._area.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
					init();
					break;
				case 5:
					//방이 없습니다
					TweenMax.to(_noRoom, 0.45, {autoAlpha:1, ease:Quart.easeInOut});
					this._area.addEventListener(MouseEvent.MOUSE_OUT, onMouseOutHandler);
					reroomList();
					init();
					break;
				default :
					
					break;
			}
		}
		private function reroomList():void
		{
			var obj:Object;
			obj = {
				"data":[
					{
						"type":_sortNum
						,"page":_page
					}
				]
			};
			DAOSendFlash.getLoadInstance().setData("reRoomList", obj);	
			
		}
		
		private function defaultSetting():void
		{
			this._gameIngClip.visible				= false;
			this._gameIngClip.alpha					= 0;
			
			this._roomFull.visible					= false;
			this._roomFull.alpha					= 0;
			
			this._gameIng.visible					= false;
			this._bg.alpha							= 0;
			this._security.stop();
			this._deem.alpha						= 0;
			this._deem.visible						= false;
			
			this._noRoom.visible					= false;
			this._noRoom.alpha						= 0;
			
			this._roomState.mouseChildren			= false;
			this._roomState.mouseEnabled			= false;
			
			var textformat_letterspacing:TextFormat = new TextFormat();
			textformat_letterspacing.letterSpacing 	= -1;
			
			var titleformat:TextFormat				= new TextFormat();
			titleformat.letterSpacing 				= -1;
			titleformat.bold						= true;
			
			this._title.setTextFormat(titleformat);
			this._roomName.setTextFormat(textformat_letterspacing);
			this._roomState.room_txt.setTextFormat(textformat_letterspacing);
			
			this._white.mouseChildren 				= false;
			this._white.mouseEnabled 				= false;
		}
	}
}
